<template>
	<view class="collection">
		<!-- <view class="tab-view">
			<u-tabs :list="list" active-color="#141414" height="100" bar-width="24" font-size="28" :bold="true" inactive-color="#757575"
			 :is-scroll="false" :current="current" @change="change"></u-tabs>
		</view> -->

		<view class="collection-content">
			<view class="collection-list">
				<view v-if="current != 3" v-for="(item,index) in collectionList" :key="item.collect_id">
					<view class="collection-item" @click="goGoodsDetails(item.goods_id,item.goods,item.status)">
						<image class="goods-img" :src="item.goods.image_middle || item.goods.image" mode=""></image>
						<view class="text-info">
							<view class="goods-name">{{item.goods.goods_name}}</view>
							<view class="price" v-if="current!=2"><text>¥</text>{{item.goods.retail_price}}</view>
							<view class="clik">
								人气{{item.goods.click}}
								<view class="cancelCollection" @click.stop="click(index,item.status)">取消收藏</view>
							</view>
						</view>
					</view>
				</view>
				<view v-if="current == 3" v-for="(item,index) in collectionList" :key="item.collect_id">
					<view class="collection-item" @click="goCase(collectionList[index].case_id)">
						<image class="goods-img" :src="item.image_middle" mode=""></image>
						<view class="text-info">
							<view class="goods-name">{{item.case_name}}</view>
							<view class="price">{{item.space}} | {{item.style_name}}</view>
							<view class="clik">
								人气{{item.click_mun}}
								<view class="cancelCollection" @click.stop="click(index)">取消收藏</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<pt-nothing
				text="暂无收藏" 
				:fixed="true" 
				v-if="noMore && !collectionList.length"
				icon="https://jiajudashi.oss-cn-shenzhen.aliyuncs.com/images/shop/icons/nothing.png?v=1">
			</pt-nothing>
			<pt-loading-more v-if="loading"></pt-loading-more>
			<pt-nomore v-if="noMore && collectionList.length"></pt-nomore>
		</view>
		<pt-loading></pt-loading>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: 0,
				list: [{
					name: '商品'
				}],
				noMore: false,
				loading: false,
				current: 0,
				collectionList: [],
			};
		},
		onLoad() {
			this.getData();
		},
		methods: {
			getData() {
				if(this.current != 3){
					let status
					switch (this.current){
						case 0:
							status = 1
							break;
						case 1:
							status = 2
							break;
						case 2:
							status = 4
							break;
					}
					let data = {
						status: status,
						page: this.page
					}
					this.$u.api.CollectItems(data).then(res => {
						this.loading = false
						if(res.datas.list.length){
							res.datas.list.forEach((item) => {
								this.collectionList.push(item)
							})
						}else{
							this.noMore = true
						}
					})
				}else {
					let data = {
						page: this.page,
						limit: 10,
					}
					this.$u.apiWei.CaseCollectList(data).then(res => {
						this.loading = false
						if(res.datas.list.length){
							res.datas.list.forEach((item) => {
								this.collectionList.push(item)
							})
						}else{
							this.noMore = true
						}
					})
				}
			},
			change(index) {
				this.current = index;
				this.page = 0
				this.noMore = false
				this.collectionList = []
				this.getData()
			},
			// 取消收藏
			click(index,status) {
				if (this.current == 3) {
					let data = {
						case_id: this.collectionList[index].case_id,
					}
					this.$u.apiWei.CaseCollect(data).then(res => {
						this.collectionList.splice(index, 1);
						if(!this.collectionList.length){
							this.noMore = true
						}
						this.$u.toast(res.datas);
					})
				} else {
					let data = {
						goods_id: this.collectionList[index].goods_id,
						status: status
					}
					this.$u.apiWei.UpCollect(data).then(res => {
						this.collectionList.splice(index, 1);
						if(!this.collectionList.length){
							this.noMore = true
						}
						this.$u.toast(res.datas);
					})
				}
			},
			// 去商品详情或者套餐详情
			goGoodsDetails(goods_id, goods, status) {
				switch (status){
					case 1:
						getApp().goProInfo(goods_id)
						break;
					case 2:
						if(goods.is_house==2){
							uni.navigateTo({
								animationDuration: 500,
								url: '/pages/package/house-info?group_id='+goods_id
							})
						}else{
							uni.navigateTo({
								animationDuration: 500,
								url: '/pages/package/n98-info?group_id='+goods_id
							})
						}
						break;
					case 3:
						getApp().goProInfo(goods_id,true)
						break;
					case 4:
						uni.navigateTo({
							animationDuration: 500,
							url: '/brand-list/brand-info/brand-info?type=1&factory_id='+goods_id
						})
						break;
				}
			},
			goCase(id){
				uni.navigateTo({
					animationDuration: 500,
					url: '/pages/case-list/case-details/case-details?case_id=' + id
				})
			}
		},
		onReachBottom() {
			if(!this.noMore){
				this.page++
				this.loading = true
				this.getData()
			}
		},
		async onPullDownRefresh() {
			this.noMore = false
			this.page = 0
			this.collectionList = []
			await this.getData()
			uni.stopPullDownRefresh();
			uni.showToast({
				icon: 'none',
				title: '刷新成功'
			})
		}
	}
</script>

<style lang="scss">
	.collection {
		.tab-view {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 2;
			border-bottom: solid 2rpx #EEE;
		}

		.collection-content {
			// padding-top: 100rpx;

			.collection-list {
				padding-top: 20rpx;

				.collection-item {
					padding: 30rpx 30rpx;
					display: flex;
					align-items: center;
					.goods-img {
						display: block;
						width: 220rpx;
						height: 220rpx;
						border-radius: 8rpx;
					}

					.text-info {
						flex: 1;
						padding-left: 30rpx;

						.goods-name {
							font-size: 28rpx;
							color: #2F2F2F;
							overflow: hidden;
							text-overflow: ellipsis;
							display: flex;
							display: -webkit-box;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical;
						}

						.price {
							margin-top: 40rpx;
							font-size: 26rpx;
							font-weight: bold;
							color: #141414;

							text {
								font-size: 20rpx;
							}
						}

						.clik {
							margin-top: 30rpx;
							font-size: 22rpx;
							color: #9E9E9E;
							display: flex;
							justify-content: space-between;
							align-items: center;
							.cancelCollection{
								background-color: #F59359;
								color: #FFF;
								border-radius: 8rpx;
								padding: 10rpx 20rpx;
							}
						}
					}

				}
			}
		}
	}
</style>
